package com.wy.framework.actions.common;

import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.Action;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.constants.RtnCodeConstant;
import com.wy.framework.exceptions.ServiceException;
import com.wy.framework.services.common.FileService;

/**
 * 项目名称：openportal
 * 类名称：FileDownloadAction
 * 类描述：下载文件通用控制器
 * 创建人：张旭星
 * 创建时间：2015-12-28 下午7:38:47
 * 修改人：张旭星
 * 修改时间：2015-12-28 下午7:38:47
 * 版本信息：@version 1.0
 * Copyright (c) 2015万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version 
 */
public class FileDownloadAction extends AbstractBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 日志操作符声明
	private static Logger logger = LoggerFactory
			.getLogger(FileDownloadAction.class);

	// 依赖的业务逻辑组件
	@Autowired
	private FileService fileService;

	/**
	 *文件下载相关
	 */
	private String fileCode;
	private String fileClass;
	private String fileName;
	private InputStream inputStream = null;

	@Override
	public String execute() throws ServiceException{
		if (logger.isInfoEnabled()) {
			logger.info("=============>> FileDownloadAction start ......");
		}

		try {
			setInputStream(fileService.getDowloadFile(fileCode));
			fileName=new String(fileService.getFileName(fileCode).get("fileName").toString().getBytes("GBK"),"ISO-8859-1");
		} catch (ServiceException e) {
			rspCode = e.getRspCode();
			rspMsg = e.getRspMsg();
			throw new ServiceException(rspCode, rspMsg);
		} catch (Exception e) {
			rspCode = RtnCodeConstant.RTN_CODE_UNKNOW_ERROR;
			rspMsg = msgSourceAdapter.getMessage(rspCode);
		}

		if (logger.isInfoEnabled()) {
			logger.info("=============>> FileDownloadAction end ......");
		}

		return Action.SUCCESS;
	}

	public String getFileCode() {
		return fileCode;
	}

	public void setFileCode(String fileCode) {
		this.fileCode = fileCode;
	}

	public String getFileClass() {
		return fileClass;
	}

	public void setFileClass(String fileClass) {
		this.fileClass = fileClass;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
}
